package com.weitu.occupationalHealth.facility.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.occupationalHealth.facility.service.FacilityService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;
/**
 * Created by WT-SUN on 2017/4/10.
 */
@Controller
@RequestMapping(value = "occupationalHealth/facility")
public class FacilityController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "occupationalHealth/facility/";

    @Autowired
    private AccessoryUtilService accessoryUtilService;

    @Autowired
    private FacilityService facilityService;

    /**
     * 跳转至设施页面
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public String list() {
        return MODULE_BATH_PATH + "list";
    }

    @RequestMapping(value = "/a/list")
    public String alist() {
        return MODULE_BATH_PATH + "analysis";
    }

    /**
     * 新增页面
     *
     * @return
     */
    @RequestMapping(value = "/add")
    public String add() {
        return MODULE_BATH_PATH + "add";
    }

    /**
     * 跳转详情与编辑页面
     *
     * @param id
     * @return
     */
    @RequestMapping("info")
    public ModelAndView info(String id, String type) {
        ModelAndView modelAndView = new ModelAndView();
        Map map= facilityService.selectById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        modelAndView.addObject("fact", map);
        modelAndView.addObject("accessoryList",accessoryList);
        if (type.equals("info")) {
            modelAndView.setViewName(MODULE_BATH_PATH + "info");
        } else {
            modelAndView.setViewName(MODULE_BATH_PATH + "edit");
        }
        return modelAndView;
    }

    /**
     * 添加防护设施
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("insertSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——防护设施", methods = "添加防护设施")
    public AjaxResult insertSelective(){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState",0);
        int boo=facilityService.insertSelective(paramMap);
        if (boo > 0) {
            boolean accFlag= AccessoryUtil.saveAccerssoryInfo(paramMap.get("id").toString());
            if (accFlag){
                return success("添加成功");
            }else {
                return fail("添加失败");
            }
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 更新防护设施
     * @return
     */
    @ResponseBody
    @RequestMapping("updateByIdSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——防护设施", methods = "更新防护设施")
    public AjaxResult updateByIdSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getEditBasicFieldMap(paramMap);
        int boo=facilityService.updateByIdSelective(paramMap);
        if (boo > 0) {
            boolean accFlag= AccessoryUtil.saveAccerssoryInfo(paramMap.get("id").toString());
            if (accFlag){
                return success("更新成功");
            }else {
                return fail("更新失败");
            }
        } else {
            return fail("更新失败");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——防护设施", methods = "删除防护设施")
    public AjaxResult deleteById(String id){
        int boo=facilityService.deleteById(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    /**
     * 防护设施列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/facilitiesTable", method = RequestMethod.POST)
    public Object facilitiesTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum=facilityService.selectFacilitiesCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,totalNum);
        PageView pageView=dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Map> maps=facilityService.selectAllFacilities(paramMap);
        Object data=dataGridPagination.flushDataGridData(maps);
        return data;
    }

    /**
     * 防护设施统计分析
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/selectAnalysis", method = RequestMethod.POST)
    public List<Map<String,Object>> selectAnalysis(HttpServletRequest request)throws Exception{
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        List<Map<String,Object>> records =facilityService.selectAnalysis(paramMap);
        return records;
    }
}
